package com.example.administrator.comzhouqing_test;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    private TextView title;
    private TextView back_btn,register_btn,tv_find_psw;
    private Button login_btn;
    private String userName,psw,spPsw;
    private EditText et_user_name,et_psw;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
    }
    private void init(){
        title=(TextView) findViewById(R.id.title);
        title.setText("登录");
        back_btn=(TextView) findViewById(R.id.back_btn);
        register_btn=(TextView) findViewById(R.id.register_btn);
        tv_find_psw=(TextView) findViewById(R.id.tv_find_psw);
        login_btn=(Button) findViewById(R.id.login_btn);
        et_user_name=(EditText) findViewById(R.id.et_user_name);
        et_psw=(EditText) findViewById(R.id.et_psw);
        back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoginActivity.this.finish();
            }
        });
        register_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
                tv_find_psw.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                });
                login_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        userName=et_user_name.getText().toString().trim();
                        psw=et_psw.getText().toString().trim();
                        if (TextUtils.isEmpty(userName)){
                            Toast.makeText(LoginActivity.this,"请输入用户名",
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }else if (TextUtils.isEmpty(psw)){
                            Toast.makeText(LoginActivity.this,"请输入密码",
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }else if (md5Psw.equals(spPsw)){
                            Toast.makeText(LoginActivity.this,"登录成功",
                                    Toast.LENGTH_SHORT).show();
                            saveLoginStatus(true,userName);
                            Intent data=new Intent();
                            data.putExtra("isLogin",true);
                            setResult(RESULT_OK,data);
                            LoginActivity.this.finish();
                            return;

                        }else if ((!TextUtils.isEmpty(spPsw)&&!
                                md5Psw.equals(spPsw)));

                    }
                });
            }
        });
    }
}
